<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('初始化数据') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('模块名称')" prop="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          :placeholder="$t('模块名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['init:init:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
        <el-button
          v-hasPermi="['init:init:edit']"
          type="primary"
          size="mini"
          @click="packageDownload"
        >{{ $t('下载模板') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="initDataList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('模块名称')" align="center" prop="moduleName" />
      <el-table-column :label="$t('当前数据量')" align="center" prop="count" width="120" />
      <el-table-column :label="$t('导入顺序')" align="center" prop="importSort" width="80" />
      <el-table-column :label="$t('备注')" align="center" prop="remark" />
      <el-table-column :label="$t('操作')" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['init:init:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="downloadHandle(scope.row)"
          >{{ $t('下载模板') }}</el-button>
          <el-button
            v-hasPermi="['init:init:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="uploadHandle(scope.row)"
          >{{ $t('导入数据') }}</el-button>
          <el-button
            v-hasPermi="['init:init:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['init:init:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改初始化数据对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <initDataForm ref="initDataForm" />
    </el-dialog>

    <!-- 导入窗口 -->
    <el-dialog :title="$t('导入')" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false">
      <div>
        <el-upload
          ref="upload"
          v-loading="upload.isUploading"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-exceed="handleFileExceed"
          :on-change="handleFileChange"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            {{ $t('将文件拖到此处，或') }}
            <em>{{ $t('点击上传') }}</em>
          </div>
          <div slot="tip" class="el-upload__tip" style="color:red">
            {{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}
          </div>
        </el-upload>

        <div slot="footer" class="dialog-footer" style="text-align: right;">
          <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      custom-class="menu-dialog-height"
    >

      <!-- <div v-html="htmlMsg" /> -->
      <el-table
        :data="tableData"
        style="width: 100%"
        height="500"
        border
        :show-header="false"
      >
        <el-table-column
          prop="msg"
          label="错误信息"
        >
          <template slot-scope="scope">
            <el-tooltip :content="scope.row.msg">
              <span>{{ scope.row.msg }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { listInitData, delInitData, exportInitData } from '@/api/init/initData'
import initDataForm from './initDataForm.vue'
import { getToken } from '@/utils/auth'
import { downLoadFileOpen } from '@/utils/zipdownload'
import { config } from '@/utils'
export default {
    components: {
        initDataForm
    },
  data() {
      return {
          dialogVisible: false,
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 初始化数据表格数据
          initDataList: [],
          // 弹出层标题
          title: '初始化数据',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              moduleName: undefined,
              implClassName: undefined,
              count: undefined,
              importSort: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 项目导入参数
          upload: {
            // 是否显示弹出层（项目导入）
            open: false,
            // 弹出层标题（项目导入）
            title: '',
            // 是否禁用上传
            isUploading: false,
            // 设置上传的请求头部
            headers: { Authorization: 'Bearer ' + getToken() },
            // 上传的地址
            url: ''
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          htmlMsg: '',
          tableData: []
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询初始化数据列表 */
    getList() {
        this.loading = true
        listInitData(this.queryParams).then(response => {
            this.initDataList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,

            moduleName: undefined,

            implClassName: undefined,

            count: undefined,

            importSort: undefined,

            remark: undefined

        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.initDataForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.initDataForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delInitData(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportInitData(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    downloadHandle(row) {
      downLoadFileOpen('/init/initData/download/' + row.implClassName + '/' + row.templateName)
    },
    packageDownload() {
      window.open(config.baseURL + '/init/initData/packageDownloadInit?Authorization=' + getToken(), '_blank')
    },
    uploadHandle(row) {
      this.upload.open = true
      this.upload.url = process.env.VUE_APP_BASE_API + '/init/initData/upload' + '/' + row.implClassName
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      if (response.msg === 'success') {
        this.msgSuccess(this.$t('导入成功'))
      } else if (response.code === 500) {
        this.$notify({
          title: this.$t('提示'),
          message: this.$t(response.msg),
          duration: 0
        })
      } else {
        // 更换为dialog
        this.dialogVisible = true
        this.tableData = response.data
        // this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      }
      // this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 文件达到上限处理
    handleFileExceed() {
      this.msgError(this.$t('可上传文件数量已达到上限'))
    },
    // 文件上传变更时间
    handleFileChange(file, fileList) {
      if (!(file.name.indexOf('xls') >= 0 && file.name.indexOf('xlsx') >= 0)) {
        this.msgError(this.$t('仅允许导入“xls”或“xlsx”格式文件！'))
        fileList.splice(0, 1)
      }
    }
  }
}
</script>
<style scoped>

.menu-dialog-height {
       height: 56%;

}

</style>
